<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
use src\logic\limapi;
use src\logic\lrobots;
class Cvideo extends controller {
	
	public function main() {
		$view   = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang  = lang::inst('dd');
		
		$config = C\config::load('config', false);
		$view->assign_by_ref('config', $config);
		$view->assign('reason', $dlang->get('log0003'));
		$view->assign('duration', $dlang->get('log0002'));
		$view->assign('title', Cadmin::$name);
		$view->display('live/monitor/video.ext.js');
	}
	
	public function dowarn() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id    = input::post('ids');
		$say   = input::post('say');
		if (empty($say)) {
			$view->assign_error('1', "Can't be empty");
		}
		$orm   = C\model::load('pro.pl.studio');
		$table = $orm->get_table();
		$studio= $orm->get_orm()->clear()->table($table)->where('id', $id)->get_row();
		$msg   = json_encode(array('msg'=>$say, 'uin'=>$studio['userid']));
		limapi::notifystudio($id, $msg, $studio['userid'], 20030);
		$field = array('warn'=>(int)$studio['warn'] + 1);
		$orm->get_orm()->clear()->table($table)->where('id', $id)->update($field);
		$orm->set_ver();
		$view->display();
	}
	
	public function replay() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$where = array('olstatus'=>1, 'status'=>1);
		$ids   = C\model::$cacher->get('adm_rand_monitor');
		if ($ids) {
			$where['id'] = array($ids, \classlib\orm::OP_NOTIN);
		} else {
			$ids = array();
		}
		$limit = 9;
		$fields= 'id,studiono,title,author,userid,warn';
		$list  = C\model::load('pro.pl.studio')
			->get_list(0, $limit, $where, $fields, 'neartime', \classlib\orm::DESC);
		if (empty($list) && $ids) {//一轮都取完了重新一轮开始
			unset($where['id']);
			$list  = C\model::load('pro.pl.studio')
				->get_list(0, $limit, $where, $fields, 'neartime', \classlib\orm::DESC);
			$ids   = array();
		}
		//获取数据资料信息
		if ($list) {
			$config = C\config::load('config', false);
			$aorm   = C\model::load('pro.pl.anchor');
			$sorm   = C\model::load('pro.inc.star');
			foreach ($list as &$item) {
				$item['link']   = $config['website'].C\router::get_url('live', $item['studiono']);
				$anchor = $aorm->get_one($item['userid']);
				$star   = $sorm->get_one($anchor['starlv']);
				$item['starlv'] = $star['name'];
				$ids[]          = $item['id'];
			}
		} else {
			$list = array();
		}
		C\model::$cacher->set('adm_rand_monitor', $ids);
		$view->assign_by_ref('list', $list);
		$view->display();
	}
	
	public function viewer() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit) = Cadmin::offset();
		$uids  = trim(input::get_post('s'));
		$uids  = explode(',', $uids);
		$uids  = array_filter($uids, 'is_numeric');
		$uids  = array_unique($uids);
		$total = 0;
		$list  = array();
		if ($uids) {
			$where = ' where userid in('.implode(',', $uids).')';
			$user  = trim(input::get_post('user'));
			if ($user) {
				$where .= ' AND nickname like "%'.$user.'%"';
			}
			$field = 'userid, nickname, thumb, lv, exp, email, emailverify, regtime, lasttime, lastip, attachs';
			$tables= C\model::load('pro.pl.userinfo')->db_table();
			array_shift($tables);
			$sqls  = array();
			foreach ($tables as $table) {
				$sqls[] = 'select '.$field.' from '.$table.$where;
			}
			$orm   = C\model::load('pro.pl.userinfo')->get_orm();
			$tsql  = 'select count(1) from('.implode(' UNION ', $sqls).') as a';
			$total = $orm->get_value($tsql);
			if ($total > 0) {
				$sql  = implode(' UNION ', $sqls);
				$list = $orm->get_list($sql, $start, $limit);
				$config = C\config::load('config', false);
				$key  = 'admin_say_';
				$rds  = \classlib\cache::inst('redis');
				foreach ($list as &$item) {
					if (!empty($item['attachs'])) {
						$info = json_decode($item['attachs'], 1);
						$item['email']    = $info['realEmail'];
						$item['nickname'] = mb_convert_encoding(base64_decode($info['nickname']), 'utf-8');
					}
					$item['thumb']    = $config['website'].$item['thumb'];
					$item['regtime']  = date('Y-m-d H:i:s', $item['regtime']);
					$item['lasttime'] = date('Y-m-d H:i:s', $item['lasttime']);
					$item['status']   = $rds->get($key.$item['userid']) ? 'No' : 'Yes';
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	public function dosay() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::get_post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids   = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			exit;
		}
		$status = input::get_post('status');
		$key = 'admin_say_';
		$rds  = \classlib\cache::inst('redis');
		//1-禁言 2-解除
		if ($status == 1) {
			foreach ($ids as $id) {
				$rds->set($key.$id, 1, 3600*2);
			}
		} else {
			foreach ($ids as $id) {
				$rds->delete($key.$id);
			}
		}
		$view->display();
	}
	
	//重置直播间机器人资料信息
	public function reset() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids  = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			$view->assign_error('1', '请选择要处理的记录');
		}
		foreach ($ids as $id) {
			lrobots::reset((int)$id);
			limapi::delrobotlist((int)$id);
		}
		$view->display();
	}
	
	/**
	 * @desc    给主播设置机器人信息
	 * @doc     给主播增加机器人
	 * @access  需要授权
	 */
	public function addrobots() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$sid     = (int)input::get_post('s');
		$count   = (int)input::get_post('count');
		$time    = (int)input::get_post('time');
		if ($sid > 0) {
			$res = lrobots::add($sid, $count, $time);
		}
		$view->display();
	}
}